package com.t2t.top.framework.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URL;

/**
 * Page辅助类
 *
 * @author ypf
 */
public class PageUtils {

    private static Logger logger = LoggerFactory.getLogger(PageUtils.class);

    /**
     * 客户端输出文本
     */
    public static void WriteReponseText(HttpServletResponse response, String info) {

        response.reset();
        response.setContentType("text/html;charset=UTF-8");
        response.setHeader("Cache-Control", "no-cache");
        try {
            response.getWriter().write(info);
            response.getWriter().flush();
        } catch (IOException e) {
            logger.info("输出错误", e);
        }
    }

    /**
     * 客户端下载
     *
     * @@param bytes 内容
     * @@param fileName 文件名
     */
    public static void clientDown(HttpServletRequest request, HttpServletResponse response, String content, String fileName) throws IOException {

        response.setHeader("Expires", "0");
        response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
        response.setHeader("Pragma", "public");
        response.setContentType("application/vnd.ms-excel");
        /* 如果想出来让IE提示你是打开还是保存的对话框，加上下面这句即可 */
        response.setHeader("Content-disposition", "attachment; filename=" + fileName);

        PrintWriter writer = response.getWriter();
        writer.write(content);
        writer.flush();
        writer.close();
    }


    public static void WriteReponseJson(HttpServletResponse response, Object data) {
        String info = GsonUtils.toJson(data);
        response.reset();
        response.setContentType("text/json; charset=UTF-8");
        response.setHeader("Cache-ControQl", "no-cache");
        try {
            response.getWriter().write(info);
            response.getWriter().flush();
        } catch (IOException e) {
            logger.info("输出错误", e);
        }
    }

    /**
     * 支持在线打开文件的一种方式
     */
    public static void downLoad(String filePath, HttpServletResponse response, boolean isOnLine) throws Exception {

        File f = new File(filePath);
        if (!f.exists()) {
            response.sendError(404, "File not found!");
            return;
        }
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(f));
        byte[] buf = new byte[1024];
        int len = 0;

        response.reset(); // 非常重要
        if (isOnLine) { // 在线打开方式
            URL u = new URL("file:///" + filePath);
            response.setContentType(u.openConnection().getContentType());
            response.setHeader("Content-Disposition", "inline; filename=" + f.getName());
            // 文件名应该编码成UTF-8
        } else { // 纯下载方式
            response.setContentType("application/x-msdownload");
            response.setHeader("Content-Disposition", "attachment; filename=" + f.getName());
        }
        OutputStream out = response.getOutputStream();
        while ((len = in.read(buf)) > 0)
            out.write(buf, 0, len);

        ToolsIoUtils.closeQuietly(in);
        ToolsIoUtils.closeQuietly(out);
    }

}